package com.nashuju.web.service.user;

import java.awt.List;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.ibatis.annotations.Param;
import org.durcframework.core.service.CrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nashuju.web.dao.user.UserDao;
import com.nashuju.web.entity.user.User;
import com.nashuju.web.service.sw.STaskService;
import com.nashuju.web.util.PasswordUtil;

@Service
public class UserService extends CrudService<User, UserDao> {

	/**
	 * 重置用户密码
	 * @param user
	 * @return 返回明文密码
	 */
	public String resetUserPassword(User user){
		
		String password = createNewPswd();
		
		String hash = PasswordUtil.createStorePswd(password);
		
		user.setPassword(hash);
		
		this.update(user);
		
		return password;
	}
	
	public void updateUserPassword(User user,String md5Pswd){    	
		user.setPassword(PasswordUtil.createHash(md5Pswd));    	
    	update(user);
	}
	
	/**
	 * 生成随机密码,由三个小写字母+三个数字组成
	 * @return
	 */
	public static String createNewPswd(){
		StringBuilder pswd = new StringBuilder();
		
		// 随机三个小写英文字母
		for (int i = 0; i < 3; i++) {
			// ascii码 97~122
			char ascii = (char) ((int)(Math.random() * 26) + 97);
			pswd.append(ascii);
		}
		// 随机三个1~9的数
		for (int i = 0; i < 3; i++) {
			int num = (int)(Math.random() * 9) + 1;
			pswd.append(num);
		}
		
		return pswd.toString();
	}
	
	public User getByUserName(String username) {
		if (this.getDao().getByUserName(username).get(0)!=null) {
			
			return  this.getDao().getByUserName(username).get(0);
		}
		else {
			return null;
		}
		
	}

		
	
	
	public static void main(String[] args) {
		System.out.println(createNewPswd());
	}
	
}
